package com.dyp.demo.service;

import com.dyp.demo.util.SomeTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.sql.SQLException;

@Slf4j
@Service
@AllArgsConstructor
public class ServiceBusinessA{

    final ServiceBasic serviceBasic;
    public String getInfo(String id){
        // 背景信息已经通过message参数传入，所以不需要try catch
        Assert.hasText(id, "参数id不能为空");
        return "OK";
    }

    public String getInfoWithoutBackgrounds(String id) throws SQLException {
        // 会完全丢失这里的调用背景信息，比如id的信息。
        SomeTools.doSomeLogic();
        return "OK";
    }

    public String getInfoWithBackgrouds(String id) {
        try {
            SomeTools.doSomeLogic();
        } catch (SQLException e) {
            // 1. 将受检异常转换为非受检异常
            // 2. 将背景信息带入
            // 3. 日志记录关键点
            String errorInfo = String.format("you can add description information and " +
                    "bring out the the backgrounds information! input: id = %s; error inf: %s", id, e.getMessage());

            log.error(errorInfo);
            throw new RuntimeException(errorInfo, e);
        }
        return "OK";
    }

}
